#!/usr/bin/python
#-*-coding:utf-8-*-

from ConfigParser import RawConfigParser
from os.path import isfile

class iniParse():
    def __init__(self,file = 'script',path = 'config/'):
        self.file = '%s%s.ini' % (path,file)
        if isfile(self.file):
            self.initialize = RawConfigParser()
            self.initialize.read(self.file)
        else:
            print 'file %s is not found!' % self.file
            quit()
        self.int = ['diftime','execnum','runnums']
        self.arr = ['putargs']


    def get(self, name = None , key = None ,type = 'string'):
        if name is None and key is None:
            data = {}
            items = self.initialize.sections()
            for i in items:
                data[i] = self.get(i)
            return data
        if key is None:
            data = {}
            items = self.initialize.items(name)
            for i in items:
                if i[0] in self.int:
                    data[i[0]] = self.get(name,i[0],'int')
                elif i[0] in self.arr:
                    data[i[0]] = self.get(name,i[0]).split(',')
                else:
                    data[i[0]] = i[1]
            return data
        else:
            if type == 'int':
                return self.initialize.getint(name,key)
            elif type == 'float':
                return self.initialize.getfloat(name,key)
            elif type == 'boolean':
                return self.initialize.getboolean(name,key)
            else:
                return self.initialize.get(name,key)
